package com.yz.controller;

import com.github.pagehelper.Page;
import com.yz.entity.Categorys;
import com.yz.entity.Product;
import com.yz.entity.Provider;
import com.yz.service.CategorysService;
import com.yz.service.ProductService;
import com.yz.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class ProductController {

    @Autowired
    ProductService productService;
    @Autowired
    ProviderService providerService;
    @Autowired
    CategorysService categorysService;
    @RequestMapping("/productList.action")
    public ModelAndView showAll(@RequestParam(value = "pageIndex",required = false,defaultValue = "1") int pageIndex){
        ModelAndView mav = new ModelAndView();
        Page<Product> products = productService.getAllProducts(pageIndex);
        long total = products.getTotal();//总记录数
        int pages = products.getPages();//总页数
        mav.addObject("pageIndex",pageIndex);
        mav.addObject("total",total);
        mav.addObject("pages",pages);
        mav.addObject("products",products);
        mav.setViewName("WEB-INF/admin/product/product_list.jsp");
        return mav;
    }

    @RequestMapping("/toAddProduct.action")
    public ModelAndView addProduct(){
        ModelAndView mav = new ModelAndView();
        List<Provider> providers = providerService.getAll();
        List<Categorys> categorys = categorysService.getAll();
        mav.addObject("providers",providers);
        mav.addObject("categorys",categorys);
        mav.setViewName("WEB-INF/admin/product/product_toAdd.jsp");
        return mav;
    }

    @RequestMapping("/doAddProduct.action")
    public ModelAndView doAddProduct(Product product){
        ModelAndView mav = new ModelAndView();
        int result = productService.addProduct(product);
        if (result == 1){
            mav.setViewName("productList.action");
            return mav;
        }else{
            return mav;
        }
    }

    @RequestMapping("/doDelProduct.action")
    public ModelAndView doDelProduct(int productId){
        ModelAndView mav = new ModelAndView();
        int result = productService.removeProductById(productId);
        if (result == 1){
            mav.setViewName("productList.action");
            return mav;
        }else{
            return mav;
        }
    }

    @RequestMapping("/toEditProduct.action")
    public ModelAndView toEditProduct(int productId){
        ModelAndView mav = new ModelAndView();
        List<Provider> providers = providerService.getAll();
        List<Categorys> categorys = categorysService.getAll();
        Product product = productService.getProductById(productId);
        mav.addObject("product",product);
        mav.addObject("providers",providers);
        mav.addObject("categorys",categorys);
        mav.setViewName("WEB-INF/admin/product/product_toEdit.jsp");
        return mav;
    }

    @RequestMapping("/doEditProduct.action")
    public ModelAndView doEditProduct(Product product){
        ModelAndView mav = new ModelAndView();
        int result = productService.EditProduct(product);
        mav.setViewName("productList.action");
        return mav;
    }
}
